import numpy as np
import math
import os
import pandas as pd
import subprocess
import time
from tough_py import extract
import scipy.io as scio

NPar = 50
dimension=78
tracerpoints=61
def bd(x):
	for i in range(0, x.shape[0]):
		if x[i, 0] < 1:
			x[i, 0] = 1
		if x[i, 0] > 1000:
			x[i, 0] = 1000
		if x[i, 1] < 2:
			x[i, 1] = 2
		if x[i, 1] > 5:
			x[i, 1] = 5
	return x

def kaidukringing(z):
	matlab_alldata = scio.loadmat('matlab_all2.mat')
	FF = matlab_alldata['FF']
	Cursy = matlab_alldata['Cursy'].T
	z = z.T


	Khat = -3
	kaidu=np.zeros([NPar,50,50])
	for ii in range(0,NPar):
		FFF = np.zeros([50, 50, dimension])
		for i in range(0, dimension):
			FFF[:, :, i] = z[i, ii] * FF[:, :, i]
		RT = np.zeros([50, 50])
		for j in range(0,dimension):
			RT = RT + FFF[:, :, j]
		K = Khat + RT
		kaidu[ii,:,:] = K[:,:]
	np.save("kaidu.npy", kaidu)

	return kaidu
